local skynet = require("skynet")
local cluster = require "skynet.cluster"
local math_rand = math.random
local msghelper = require "tablehelper"
local timer = require "timer"
local filelog = require "filelog"
local itemconfig = require "itemconfig"
local script = {
    [1] = function(seat)    -- 锁定状态
        seat.shootstat = 1
        msghelper:sendmsg_to_alltableplayer("shootstatN", {
            rid = seat.rid,
            seat_index = seat.index,
            shootstat = seat.shootstat,
            timelen = 20,
        })
        -- seat.timer_id = timer.settimer(20 * 100, "changeshootstat", {seat_index = seat.index})
    end,
}

script["add"] = function(seat, type, n, scriptid)
    if seat.items[type] then
        seat.items[type].num = seat.items[type].num + n
        cluster.send("gatenode", ".router", "updateprop", seat.rid, seat.items[type])
    else
        seat.items[type] = {
            id = type,
            type = type,
            num = n,
            scriptid = scriptid
        }
        cluster.send("gatenode", ".router", "updateprop", seat.rid, seat.items[type])
    end
    msghelper:sendmsg_to_alltableplayer("updateitemN", {
        rid = seat.rid,
        seat_index = seat.index,
        item = {
            id = type,
            itype = type,
            num = seat.items[type].num,
        },
    })
end

script["del"] = function(seat, type, n)
    seat.items[type].num = seat.items[type].num - n
    cluster.send("gatenode", ".router", "updateprop", seat.rid, seat.items[type])
    msghelper:sendmsg_to_alltableplayer("updateitemN", {
        rid = seat.rid,
        seat_index = seat.index,
        item = {
            id = type,
            itype = type,
            num = seat.items[type].num,
        },
    })
end

script["drop"] = function(seat_index, dropid)
    local dropitem = itemconfig.drop[dropid]
    if not dropitem then return end
    local rand = math_rand(100)
    for i = 1, #dropitem do
        if dropitem[i + 1] then
            if rand >= dropitem[i][1] and rand < dropitem[i + 1][1] then
                if dropitem[i][2] > 0 then
                    local table_data = msghelper:get_server().table_data
                    local seat = table_data.seats[seat_index]
                    script["add"](seat, dropitem[i][2], dropitem[i][3], dropitem[i][4])
                    return
                end
            end
        else
            if rand >= dropitem[i][1] then
                if dropitem[i][2] > 0 then
                    local table_data = msghelper:get_server().table_data
                    local seat = table_data.seats[seat_index]
                    script["add"](seat, dropitem[i][2], dropitem[i][3], dropitem[i][4])
                    return
                end
            end
        end
    end
end

return script